#include "EggScene.h"
#include "CCBirdProduct.h"
#include "CCBird.h"
#include "SimpleAudioEngine.h"
#include "keypad_dispatcher/CCKeypadDelegate.h"
#include "BiboFrame.h"
#include "cocos2d.h"
#define T_DUR 1.0f
#define T_OFFSET 1000
USING_NS_CC;

cocos2d::CCArray* EggScene::birdSprites;
cocos2d::CCArray* EggScene::productOfBird;
CCMySprite* EggScene::redBag;
EggScene::EggScene(void)
{
	EggScene::eggFallingSpeed = 3;
	EggScene::officialEgg = 0;
}
EggScene::~EggScene(void)
{
	if (redBag)
	{
		redBag->release();
		redBag = NULL;
	}
	if(birdSprites)
	{
		birdSprites->release();
		birdSprites = NULL;
	}
	if(productOfBird)
	{	
		productOfBird->release();
		productOfBird = NULL;
	}
}
CCScene* EggScene::scene()
{
	// 'scene' is an autorelease object
	CCScene *scene = CCScene::create();

	// 'layer' is an autorelease object
	EggScene *layer = (EggScene*)EggScene::create();

	// add layer as a child to scene
	scene->addChild(layer);
	
	// return the scene
	return scene;
}

bool EggScene::init()
{
	//////////////////////////////
	// 1. super init first
	if ( ! BiboFrame::init())
	{
		return false;
	}

	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

	/////////////////////////////
	/*CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
		"CloseNormal.png",
		"CloseSelected.png",
		this,
		menu_selector(EggScene::menuCloseCallback));

	pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
		origin.y + pCloseItem->getContentSize().height/2));

	// create menu, it's an autorelease object
	CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
	pMenu->setPosition(CCPointZero);
	this->addChild(pMenu, 1);//*/

	//INIT VALUE//
	//currentBirdChildArray = CCArray::create();
	redBag = new CCMySprite();redBag->retain();
	birdSprites = cocos2d::CCArray::create();birdSprites->retain();
	productOfBird = cocos2d::CCArray::create();productOfBird->retain();
	realCurrentSecond = 0;
	eggFallingSpeed = 3;
	officialEgg = 0;
	previousKeyStateCode;
	EggScene::initSpriteArrWithDir1(birdSprites,"2.bird.plist");
	EggScene::initSpriteArrWithDir1(productOfBird,"3.productOfBird.plist");
	
	this->schedule( schedule_selector(EggScene::updateShowingInitScreen), 1 );
	return true;
}
void EggScene::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
	CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	exit(0);
#endif
#endif
}
void EggScene::updateShowingInitScreen(float dt)
{
	BiboFrame::updateShowingInitScreen(dt);
	CCMySprite *spA,*spB;
	CCMoveTo *mov;
	CCPoint pos;
	float r = BiboFrame::realCurrentSecond;
	int timeINT = int(BiboFrame::realCurrentSecond + 0.5);
	if(timeINT==2){
		int tt = 2;
	}
	if(BiboFrame::realCurrentSecond==0)
	{
		int t = birdSprites->count();
		int t2 = sprites->count();
		BiboFrame::numCountBySecond = 0;
		//CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();  
		//CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("1.m4a",true);
		
		numCountBySecond = 0;
		//this->setKeypadEnabled(false);
		//this->setTouchEnabled(false);
		
		CCMySprite *sp170a = (CCMySprite*)EggScene::searchForSpriteById(170,EggScene::sprites);
		CCMySprite *sp170b = new CCMySprite();
		sp170b->copyValue(sp170a);
		sp170b->setPosition(ccp(sp170a->getPositionX(), sp170a->getPositionY() - sp170a->getTextureRect().size.height));
		this->addChild(sp170b,2,sp170b->getID());
		CCMoveTo *m = CCMoveTo::create(0.5,sp170a->getPreviousPosition());
		sp170b->runAction(m);
		BiboFrame::doScaleEffect(sp170b,2,sp170b->getScaleX()*1.1,sp170b->getScaleY()*1.08,0);
		
		//red bag sprite
		spA = EggScene::searchForSpriteById(160,EggScene::sprites);
		redBag = new CCMySprite();
		redBag->copyValue(spA);
		pos = ccp(spA->getPositionX(), redBag->getPositionY() - spA->getTextureRect().size.height);
		redBag->setPosition(pos);
		this->addChild(redBag,1,redBag->getID());
		mov = CCMoveTo::create(0.7,spA->getPreviousPosition());
		redBag->runAction(mov);
		//generate lives sprite showing at the top right corner
		CCArray *livesSPArray = EggScene::generatePlayerLives(160,3);
		for(int i=0;i<livesSPArray->count();i++)
		{
			this->addChild((CCMySprite*)livesSPArray->objectAtIndex(i),1,161);//tag of this sprite is 161 not 160 to be different to Red Bag sprite
		}
		////////////////////
		
	}
	else if(BiboFrame::realCurrentSecond==1)
	{
		//chicken - duck - con ngan:))
		int totalBirds = 0;
		totalBirds = EggScene::birdSprites->count();
		if(totalBirds>0){
			spA = (CCMySprite*)EggScene::birdSprites->objectAtIndex(0);
			int unitWidth = 0;
			unitWidth = (int)(EggScene::winSize.width - (2*spA->getContentSize().width*spA->getScaleX()))/(totalBirds-1);
			for(int i=0;i<totalBirds;i++)
			{
				spA = (CCMySprite*)EggScene::birdSprites->objectAtIndex(i);
				spB = new CCBird();
				CCBird *bird = new CCBird();
				bird->copyValue(spA);
				pos = ccp(spA->getContentSize().width*BiboFrame::scalePercentX + i*unitWidth , BiboFrame::winSize.height*0.8);
				bird->setPreviousPosition(pos);

				pos = ccp(pos.x, pos.y - spA->getContentSize().height*spA->getScaleY());
				bird->setPosition(pos);
				bird->setLifeTime((-2 + i)*2);
				this->addChild(bird,3,200);
				mov = CCMoveTo::create(0.5,bird->getPreviousPosition());
				bird->runAction(mov);
			}
		}
		//this->setKeypadEnabled(true);
		
	}else if(BiboFrame::realCurrentSecond==2)
	{
		EggScene::isAlreadyInit = true;
		this->unschedule(schedule_selector(EggScene::updateShowingInitScreen));
		EggScene::realCurrentSecond = 0;
		this->schedule(schedule_selector(EggScene::tick2),1/10);
		this->schedule(schedule_selector(EggScene::tick1),1);
	}else if (BiboFrame::realCurrentSecond==3){

	}else if(BiboFrame::realCurrentSecond==4)
	{        
		
	}else if (BiboFrame::realCurrentSecond==5)
	{

	}else if(BiboFrame::realCurrentSecond==6)
	{

	}else if(BiboFrame::realCurrentSecond==9)
	{
		
	}
	EggScene::realCurrentSecond++;
	
}

void EggScene::generateABird(int idGa){
	CCMySprite *spGa = BiboFrame::searchForSpriteById(idGa,EggScene::birdSprites);
	CCBird *newGa = new CCBird();
	newGa->copyValue(spGa);
	float x = 0; x = BiboFrame::getRandomNum(BiboFrame::winSize.width - (newGa->getContentSize().width*newGa->getScaleX()),newGa->getContentSize().width*newGa->getScaleX());
	float y = 0; y = this->getChildByTag(200)->getPositionY();
	newGa->setPosition(ccp(x, y));
	newGa->setPreviousPosition(newGa->getPosition());
	newGa->setLifeTime(BiboFrame::getRandomNum(-1,-10));
	this->addChild(newGa,2,200);
	BiboFrame::doScaleEffect(newGa,0.2,newGa->getScaleX()*2,newGa->getScaleY()*2,4);
}

void EggScene::keyboardInputControlProcess(float dt)
{
	//key for generate bird
	if(EggScene::isAlreadyInit){
		if(EggScene::previousKeyStateCode==0){
			int idGa = -1;
			if(GetAsyncKeyState(0x47)!=0){//G key
				BiboFrame::previousKeyStateCode = 0x47;
				idGa = 210;
			}else if(GetAsyncKeyState(0x56)!=0){//V key
				BiboFrame::previousKeyStateCode = 0x56;
				idGa = 220;
			}else if(GetAsyncKeyState(0x4E)!=0){//N key
				BiboFrame::previousKeyStateCode = 0x4E;
				idGa = 230;
			}
			if(idGa!=-1){
				EggScene::generateABird(idGa);
				this->scheduleOnce(schedule_selector(EggScene::setPreviousStateCodeToZero),2);
			}
		}
		
		//key catch for left and right move
		if(GetAsyncKeyState(0x25)!=0){
			if(redBag->getPositionX() > redBag->getContentSize().width*redBag->getScaleX()/2)
				redBag->setPositionX(redBag->getPositionX()-5);
		}else if(GetAsyncKeyState(0x27)!=0){
			if(redBag->getPositionX()+10 < BiboFrame::winSize.width-redBag->getContentSize().width*redBag->getScaleX()/2)
				redBag->setPositionX(redBag->getPositionX()+5);
		}
	}else{
		if(GetAsyncKeyState(0x52)!=0){//R key
			EggScene::stopGame(true);
		}
	}
}
void EggScene::updateLifeTimeForBirds(float dt){
	CCArray *birdChildArray = BiboFrame::searchForChildrenByTag(200);//is Ga, Ngan, Vit by tag
	//CCArray *birdChildArray = currentBirdChildArray;
	CCBird *sp;
	for(int i=0;i<birdChildArray->count();i++){
		sp = (CCBird *)birdChildArray->objectAtIndex(i);
		sp->setLifeTime(sp->getLifeTime()+1);
		if(sp->getLifeTime()>=1001)
			sp->setLifeTime(0);
	}
}

void EggScene::birdBehaviorsController(float dt)
{
	int min = 0;
	int max = EggScene::birdSprites->count()-1;
	int iNow,iNext,i1,i2;
	CCBird *sp,*sp1,*sp2;
	
	CCMoveTo *nextMov;
	CCPoint nextPosition = ccp(-1,-1);
	float widthSP;
	//CCArray *reArrangePositions = CCArray::create();
	CCArray *birdChildArray = BiboFrame::searchForChildrenByTag(200);//is Ga, Ngan, Vit by tag
	//CCArray *birdChildArray = currentBirdChildArray;
	float distanceUnit = BiboFrame::winSize.width/birdChildArray->count();
	for(int i=0;i<birdChildArray->count();i++)
	{
		sp = (CCBird*)birdChildArray->objectAtIndex(i);
		if(sp->numberOfRunningActions()==0){
			if(sp->getPositionX()<=(sp->getContentSize().width*sp->getScaleX())/2){
				nextPosition = ccp(BiboFrame::winSize.width - (sp->getContentSize().width*sp->getScaleX())/2,BiboFrame::winSize.height*0.8);
			}else {
				nextPosition = ccp((sp->getContentSize().width*sp->getScaleX())/2,BiboFrame::winSize.height*0.8);
			}
			if(nextPosition.x!=-1 && nextPosition.y!=-1){
				
				nextMov = CCMoveTo::create(((float)EggScene::getRandomNum(10,5)/10)*(
					ccpDistance(sp->getPosition(),nextPosition)
										/(sp->getContentSize().width*sp->getScaleX()))
					
					,nextPosition);
				sp->runAction(nextMov);
			}
		}
	}
}

//give new birth: egg, chat thai...dung //function
void EggScene::giveBirthControl(float dt){//1 second by step call
	//give birth
	CCArray *birdChildArray = BiboFrame::searchForChildrenByTag(200);
	//CCArray *birdChildArray = currentBirdChildArray;
	for(int i=0;i<birdChildArray->count();i++){
		CCBird *sp1 = (CCBird*)birdChildArray->objectAtIndex(i);
		if(sp1->getLifeTime()>0)
		{
			CCBirdProduct *spEgg = NULL;
			if((int)sp1->getLifeTime()%5 == 0 && this->getChildByTag(1100)==NULL){//give birth naturally mot cach tu nhien/5s for an egg
				spEgg = sp1->giveBirth();
				if(spEgg->ProductType()==1)//is egg
				{
					spEgg = new CCBirdProduct();
					spEgg->copyValue(this->searchForSpriteById(140,EggScene::sprites));
				}else if(spEgg->ProductType()==2)//is chat thai
				{
					spEgg = new CCBirdProduct();
					spEgg->copyValue(this->searchForSpriteById(150,EggScene::sprites));
				}else if(spEgg->ProductType()==3 && this->getChildByTag(190)==NULL && this->getChildByTag(1100)==NULL){
					spEgg = new CCBirdProduct();
					spEgg->copyValue(this->searchForSpriteById(190,EggScene::sprites));
				}else{
					spEgg = new CCBirdProduct();
					spEgg->copyValue(this->searchForSpriteById(140,EggScene::sprites));
				}
				BiboFrame::doScaleEffect(sp1,0.2,sp1->getScaleX()*1.3,sp1->getScaleY()*1.3,4);
			}else if(this->getChildByTag(1100)!=NULL){//only give egg

				spEgg = sp1->giveBirth(140);
				//BiboFrame::doScaleEffect(sp1,0.05,sp1->getScaleX()*1.3,sp1->getScaleY()*1.3,1);
			}
			if(spEgg!=NULL){
				spEgg->setPosition(sp1->getPosition());
				spEgg->setPreviousPosition(sp1->getPreviousPosition());
				this->addChild(spEgg,1,spEgg->getID());
				CCPoint pos = ccp(spEgg->getPositionX(),-spEgg->getContentSize().height*spEgg->getScaleY());
				CCMoveTo *mov ;
				if(this->getChildByTag(1100)==NULL)
					mov = CCMoveTo::create(EggScene::eggFallingSpeed,pos);
				else
					mov = CCMoveTo::create(3*EggScene::scalePercentY,pos);
				mov->setTag(1);
				spEgg->runAction(mov);
			}
		}
	}
}

void EggScene::reArrangeBirdProduct(){
	CCArray* arr = BiboFrame::searchForChildrenByTag(EggScene::productOfBird);
	CCMySprite *bird = (CCMySprite*)this->getChildByTag(200);
	float yBottomOfBird = (bird->getPositionY() - (bird->getContentSize().height*bird->getScaleY())/2);
	for(int i=0;i<arr->count();i++){
		CCBirdProduct *sp1 = (CCBirdProduct*)arr->objectAtIndex(i);
		for(int j=0;j<arr->count();j++){
			if(i!=j){
				CCBirdProduct *sp2 = (CCBirdProduct*)arr->objectAtIndex(j);
				if(sp1->getID()!= sp2->getID() && sp2->getPositionY() < yBottomOfBird && sp1->getPositionY() < yBottomOfBird 
					&& ccpDistance(sp1->getPosition(),sp2->getPosition())
					< ((sp1->getContentSize().width*sp1->getScaleX())/4 + (sp2->getContentSize().width*sp2->getScaleX())/4)){
						CCPoint nextPos1;
						CCPoint nextPos2;
						if(sp1->getPositionX()<sp2->getPositionX()){
							nextPos1 = ccp(sp1->getPositionX() - (sp1->getContentSize().width*sp1->getScaleX())/4,sp1->getPositionY());
							nextPos2 = ccp(sp2->getPositionX() + (sp2->getContentSize().width*sp2->getScaleX())/4,sp1->getPositionY());
							sp1->runAction(CCMoveTo::create(0.1,nextPos1));
							sp2->runAction(CCMoveTo::create(0.1,nextPos2));
						}else{
							nextPos1 = ccp(sp1->getPositionX() + (sp1->getContentSize().width*sp1->getScaleX())/4,sp2->getPositionY());
							nextPos2 = ccp(sp2->getPositionX() - (sp2->getContentSize().width*sp2->getScaleX())/4,sp2->getPositionY());
							sp1->runAction(CCMoveTo::create(0.1,nextPos1));
							sp2->runAction(CCMoveTo::create(0.1,nextPos2));
						}

						if(sp1->getPositionY() < sp2->getPositionY()){
							sp1->setPositionY(sp1->getPositionY() - (sp1->getContentSize().height*sp1->getScaleY())/2);
							sp2->setPositionY(sp2->getPositionY() + (sp2->getContentSize().height*sp2->getScaleY())/2);
						}else{
							sp1->setPositionY(sp1->getPositionY() + (sp1->getContentSize().height*sp1->getScaleY())/2);
							sp2->setPositionY(sp2->getPositionY() - (sp2->getContentSize().height*sp2->getScaleY())/2);
						}
				}
				if(sp1->getPositionY() < EggScene::redBag->getPositionY()){
					if(sp1->numberOfRunningActions()==0){//if it is not moving, this sp1 now has only one ccaction
						sp1->runAction(CCMoveTo::create(0.3*(sp1->getPositionY()+sp1->getContentSize().height*sp1->getScaleY())
							/(sp1->getContentSize().height*sp1->getScaleY())
							,ccp(sp1->getPositionX(),-sp1->getContentSize().height*sp1->getScaleY())));
					}
				}
				if(sp2->getPositionY() < EggScene::redBag->getPositionY()){
					if(sp2->numberOfRunningActions()==0){
						sp2->runAction(CCMoveTo::create(0.3*(sp2->getPositionY()+sp2->getContentSize().height*sp2->getScaleY())
							/(sp2->getContentSize().height*sp2->getScaleY())
							,ccp(sp2->getPositionX(),-sp2->getContentSize().height*sp2->getScaleY())));
					}
				}
			}
		}
	}
}
CCMySprite* EggScene::isCacthCheckProcess()
{
	CCArray *currentBirdProductArray = EggScene::searchForChildrenByTag(productOfBird);
	for(int i=0;i<currentBirdProductArray->count();i++)
	{
		CCMySprite *sp = new CCMySprite();
		sp = (CCMySprite*)currentBirdProductArray->objectAtIndex(i);
		CCRect b = sp->boundingBox();
		CCRect c = EggScene::redBag->boundingBox();
		CCRect bound = redBag->boundingBox();
		bound.setRect(bound.getMidX()- bound.size.width*0.25,bound.getMidY() - (EggScene::scalePercentY)*20, bound.size.width*0.75,EggScene::redBag->getContentSize().height*EggScene::redBag->getScaleY()*0.8);
		if(sp->isFlipX()==false && bound.intersectsRect(sp->boundingBox()))
		{
			sp->setFlipX(true);
			return sp;
		}
	}
	return NULL;
}

void EggScene::stopGame(bool isReplay){
	if(this->getChildByTag(161)==NULL)
	{
		EggScene::unscheduleAllSelectors();
		EggScene::isAlreadyInit = false;
		this->schedule(schedule_selector(EggScene::keyboardInputControlProcess),1/10);
		if(isReplay){
			EggScene::unscheduleAllSelectors();
			EggScene::removeAllChildren();
			EggScene::realCurrentSecond = 0;
			EggScene::init();
		}else{
			CCMySprite *spGameOverImg = BiboFrame::searchForSpriteById(1120,EggScene::sprites);
			if(this->getChildByTag(1120)==NULL){
				this->addChild(spGameOverImg,3);
				EggScene::doScaleEffect(spGameOverImg,0.2,spGameOverImg->getScaleX()*1.1,spGameOverImg->getScaleY()*1.1,3);
				CCMySprite *spReplayImg = BiboFrame::searchForSpriteById(1130,EggScene::sprites);
				this->addChild(spReplayImg,0,spGameOverImg->getID());
				spReplayImg->setScaleY(0);
				spReplayImg->setPositionY(spReplayImg->getPositionY() - (spReplayImg->getContentSize().height*spGameOverImg->getScaleY())/2);
				spReplayImg->setAnchorPoint(ccp(0.5,0));
				spReplayImg->runAction(CCScaleTo::create(1,EggScene::getScaleX(),EggScene::getScaleY()));
			}
			
		}
	}
}
void EggScene::tick1(float dt)//1s by step
{
	EggScene::removeAllChildrenOutOfScreen();
	EggScene::stopGame(false);
	//if(EggScene::previousKeyStateCode==0){
		
	//}
	EggScene::birdBehaviorsController(1);
	EggScene::updateLifeTimeForBirds(1);
	EggScene::giveBirthControl(1);
	
}

void EggScene::tick2(float dt)//0.1s by step
{
	if(BiboFrame::isAlreadyInit){
		EggScene::keyboardInputControlProcess(0);
		EggScene::reArrangeBirdProduct();
		EggScene::redBagOverlapEggProcess();
	}
		
}
void EggScene::moveEggToRedBag(CCNode *sender){
	if(sender==NULL)
		return;
	CCMySprite *sp = (CCMySprite*)sender;
	CCMoveTo *mov = CCMoveTo::create(0.1,EggScene::redBag->getPosition());
	sp->runAction(mov);
}
void EggScene::birdAngelGiveBirth(CCNode* sender){
	if(sender==NULL)
		return;
	CCMySprite *sp = (CCMySprite*)sender;
	CCBirdProduct *egg = new CCBirdProduct();
	egg->copyValue(EggScene::searchForSpriteById(140,EggScene::productOfBird));
	egg->setPosition(sp->getPosition());
	float time = (ccpDistance(egg->getPosition(),EggScene::redBag->getPosition())/(egg->getContentSize().height*egg->getScaleY()))*0.3;
	CCJumpTo *jump = CCJumpTo::create(time,EggScene::redBag->getPosition(),egg->getPositionY() - EggScene::redBag->getPositionY(),1);
	CCFiniteTimeAction *doneAction = CCCallFuncN::create(this,callfuncN_selector(EggScene::moveEggToRedBag));
	this->addChild(egg,3,egg->getID());
	egg->runAction(CCSequence::create(jump,doneAction,NULL));
}
void EggScene::addBirdAngel(CCNode *sender){
	if(sender==NULL)
		return;
	CCMySprite *sp = new CCMySprite();
	sp->copyValue(EggScene::searchForSpriteById(1100,EggScene::sprites));
	CCMoveTo *movToMid = CCMoveTo::create(1.5,ccp(EggScene::winSize.width/2,EggScene::winSize.height*0.7));
	CCMoveTo *movToRight = CCMoveTo::create(1.5,
		ccp(EggScene::winSize.width-(sp->getContentSize().width*sp->getScaleX())/2,sp->getPositionY()));
	CCMoveTo *movToLeft = CCMoveTo::create(1.5,
		ccp(sp->getContentSize().width/2,sp->getPositionY()));
	CCFiniteTimeAction *giveBirthAction = CCCallFuncN::create(this, callfuncN_selector(EggScene::birdAngelGiveBirth));
	CCFiniteTimeAction *lastMission = CCCallFuncN::create(this, callfuncN_selector(EggScene::moveAllEggToRedBag));
	CCSequence *seq = CCSequence::create(giveBirthAction,movToMid,giveBirthAction,movToRight,giveBirthAction,movToMid,giveBirthAction,movToLeft,NULL);
	CCMoveTo *moveOut = CCMoveTo::create(2,ccp(EggScene::winSize.width/2,-sp->getContentSize().height*2));

	this->addChild(sp,3,sp->getID());
	EggScene::doScaleEffect(sp,0.2,sp->getScaleX()*1.1,sp->getScaleY()*1.1,0);
	sp->runAction(CCSequence::create(CCRepeat::create(seq,2),lastMission,moveOut,NULL));
}
void EggScene::removeALife(){
	///remove one life icon
	CCArray *liveArray = EggScene::searchForChildrenByTag(161);
	if(liveArray->count()>0)
		this->removeChild((CCMySprite*)liveArray->objectAtIndex(liveArray->count()-1),true);
}
void EggScene::moveAllEggToRedBag(CCNode* sender){
	CCArray *otherEggs = EggScene::searchForChildrenByTag(140);
	for(int i=0;i<otherEggs->count();i++){
		CCMySprite *sp = (CCMySprite*)otherEggs->objectAtIndex(i);
		sp->stopAllActions();
		sp->setFlipX(false);
		CCJumpTo *jump = CCJumpTo::create(0.5,EggScene::redBag->getPosition(),abs(EggScene::redBag->getPositionY()-sp->getPositionY()),1);
		sp->runAction(jump);
	}
}
void EggScene::redBagOverlapEggProcess()
{
	CCMySprite* overlapedSP = EggScene::isCacthCheckProcess();
	if(overlapedSP!=NULL){
		if(overlapedSP->getID()==190){//is special egg, be going for bonus
			//jump this egg to center mid then fade out and move out of screen for being removed later.
			CCPoint pos = EggScene::searchForSpriteById(1100,EggScene::sprites)->getPosition();
			CCJumpTo *jump = CCJumpTo::create(1,pos,abs(overlapedSP->getPositionY()-pos.y)*1.1,1);
			CCFadeOut *fout = CCFadeOut::create(0.2);
			CCMoveTo *movOut = CCMoveTo::create(0,ccp(1,-1000));
			CCFiniteTimeAction* actionDone = CCCallFuncN::create(this, callfuncN_selector(EggScene::addBirdAngel));
			CCSequence *seq = CCSequence::create(jump,fout,actionDone,movOut,NULL);
			EggScene::moveAllEggToRedBag(this);
			overlapedSP->runAction(seq);
		}else{//not special egg
			CCFadeOut *fout = CCFadeOut::create(0.2);
			overlapedSP->runAction(fout);

			CCScaleTo *scale = CCScaleTo::create(0.2,0,0);
			CCMoveTo *movOut = CCMoveTo::create(0,ccp(-100,BiboFrame::winSize.height));
			CCSequence *seq = CCSequence::create(scale,movOut,NULL);
			overlapedSP->runAction(seq);

			if(overlapedSP->getID()==140){//is catch egg
				int curRecord = atoi(EggScene::recordLabel->getString());
				curRecord++;
				if(this->getChildByTag(190)==NULL && this->getChildByTag(1100)==NULL){//only for official eggs not bonus eggs
					officialEgg++;
					//UPDATE FALLING EGG SPEED UP
					if(EggScene::officialEgg > 0 && EggScene::officialEgg%20==0  && EggScene::eggFallingSpeed>1){
						float roadLength = ccpDistance(EggScene::redBag->getPosition(),this->getChildByTag(200)->getPosition());
						float timeMovingForAPixelLength = (float)(3*EggScene::scalePercentY)/roadLength;
						EggScene::eggFallingSpeed-=0.2*roadLength*timeMovingForAPixelLength;
						CCMySprite *spNotification = new CCMySprite();
						spNotification->copyValue(EggScene::searchForSpriteById(1110,EggScene::sprites));
						spNotification->setPosition(ccp(EggScene::winSize.width/2,EggScene::winSize.height/2));
						this->addChild(spNotification,3);
						CCFadeOut *fout1,*fout2;
						fout1 = CCFadeOut::create(0.2);
						fout2 = CCFadeOut::create(0.2);
						CCFadeIn *fin = CCFadeIn::create(0);
						CCMoveTo *movOut1,*movOut2;
						movOut1 = CCMoveTo::create(0,ccp(EggScene::winSize.width/2,-EggScene::winSize.height/3));
						movOut2 = CCMoveTo::create(0,ccp(-100,-EggScene::winSize.height/3));
						CCDelayTime *dl = CCDelayTime::create(0.2);
						CCSequence *seq1 = CCSequence::create(dl,fout1,fin,dl,fout1,fin,dl,movOut1,NULL);
						CCSequence *seq2 = CCSequence::create(dl,fout2,fin,dl,fout2,fin,dl,movOut2,NULL);
						spNotification->runAction(seq1);
						spNotification->setMyNotification(CCLabelTTF::create(CCString::createWithFormat("X%d",(int)(3/EggScene::eggFallingSpeed + 0.5))->m_sString.c_str(),"VNI-Whimsy",20));
						spNotification->getMyNotification()->setPosition(spNotification->getPosition());
						spNotification->getMyNotification()->setPositionY(spNotification->getPositionY() - 10*EggScene::scalePercentY);
						this->addChild(spNotification->getMyNotification(),3);
						spNotification->getMyNotification()->runAction(seq2);
						EggScene::doShakingEffect(spNotification,0.3,5,0);
					}
				}
				EggScene::recordLabel->setString(CCString::createWithFormat("%d",curRecord)->m_sString.c_str());
				EggScene::recordLabel->stopAllActions();
				EggScene::recordLabel->setScaleX(EggScene::scalePercentX);
				EggScene::recordLabel->setScaleY(EggScene::scalePercentY);
				CCScaleTo *sca1 = CCScaleTo::create(0.2,recordLabel->getScaleX()*2,recordLabel->getScaleY()*2);
				CCScaleTo *sca2 = CCScaleTo::create(0.1,recordLabel->getScaleX(),recordLabel->getScaleY());
				CCSequence *seq = CCSequence::create(sca1,sca2,NULL);
				EggScene::recordLabel->runAction(seq);
				
			}else if(overlapedSP->getID()==150){//is catch chat thai
				//CCJumpTo *jumpOut = CCJumpTo::create(2,ccp(BiboFrame::winSize.width+overlapedSP->getContentSize().width,EggScene::redBag->getPositionY()),30*BiboFrame::scalePercentY,1);
				EggScene::background->stopAllActions();
				EggScene::doShakingEffect(EggScene::background,0.05,1,3);
				//overlapedSP->runAction(jumpOut);
				///remove one life icon
				EggScene::removeALife();
			}
		}//not special egg end
	}else{//not overlap
		CCBirdProduct *egg = EggScene::isEggOutOfBottomLine();
		if(egg !=NULL){//is under red bag
			CCObject *o = EggScene::getChildByTag(180);
			if(EggScene::getChildByTag(180)==NULL){//stupid mouse moving
				CCMySprite *spStupidMouse = new CCMySprite();
				spStupidMouse->copyValue(searchForSpriteById(180,EggScene::sprites));
				this->addChild(spStupidMouse,40,spStupidMouse->getID());
				CCJumpTo *jump = CCJumpTo::create((EggScene::winSize.width/(spStupidMouse->getContentSize().width*spStupidMouse->getScaleX()))*0.3,ccp(BiboFrame::winSize.width*0.8,spStupidMouse->getPositionY()),spStupidMouse->getPositionY()+5*BiboFrame::scalePercentY,2);
				CCFadeOut *fout = CCFadeOut::create(0.1);
				CCMoveTo *movOut = CCMoveTo::create(0,ccp(-100,-100));
				CCSequence *seq = CCSequence::create(jump,fout,movOut,NULL);
				spStupidMouse->runAction(seq);
			}
			if(this->getChildByTag(1100)==NULL && this->getChildByTag(190)==NULL){//angel bird and special egg are NOT showing up now, so allow to remove life and.
				if(egg->isFlipX()==false){
					EggScene::removeALife();
					egg->setFlipX(true);
				}
			}
		}
	}

	
}
CCBirdProduct* EggScene::isEggOutOfBottomLine()
{
	CCArray *currentBirdProductArray = EggScene::searchForChildrenByTag(productOfBird);
	for(int i=0;i<currentBirdProductArray->count();i++)
	{
		CCBirdProduct *sp = new CCBirdProduct();
		sp = (CCBirdProduct*)currentBirdProductArray->objectAtIndex(i);
		if(sp->getID()==140 && sp->getPositionY()<
			EggScene::redBag->getPositionY()-1.5*(EggScene::redBag->getContentSize().height*EggScene::redBag->getScaleY()))
		{
			return sp;
		}
	}
	return NULL;
}
void EggScene::removeYourself(CCNode* sender){
	this->removeChild(sender,true);
}
void EggScene::setPreviousStateCodeToZero(float dt){
	EggScene::previousKeyStateCode = 0;
}